/**
 * @file utils
 */

var contentType = require('content-type');
var qs = require('qs');

module.exports = {

    /**
     * Set the charset in a given Content-Type string.
     *
     * @param {String} type
     * @param {String} charset
     * @return {String}
     * @api private
     */
     setCharset: function (type, charset) {
        if (!type || !charset) {
            return type;
        }

        // parse type
      var parsed = contentType.parse(type);

      // set charset
      parsed.parameters.charset = charset;

      // format type
      return contentType.format(parsed);
    },

    compileQueryParser: function (val) {
        var fn;
        if (typeof val === 'function') {
            return val;
        }

        switch (val) {
            case 'extended':
                fn = parseExtendedQueryString
                break;
            default:
                throw new Error('unknown value for query parser function: ' + val);

        }

        return fn;
    }
};


function  parseExtendedQueryString(str) {
    return qs.parse(str, {
        allowDots: false,
        allowPrototypes: true
    });
}
